package cn.pet.service.manage.controller;


import cn.pet.service.manage.entity.PetGoods;
import cn.pet.service.manage.entity.api.Result;
import cn.pet.service.manage.entity.dto.GoodsDTO;
import cn.pet.service.manage.entity.dto.UpOrDownGoodsDTO;
import cn.pet.service.manage.service.PetGoodsService;
import cn.pet.service.manage.service.PetGoodsTypeService;
import cn.pet.service.manage.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author 
 * @since 2023-02-11
 */
@RestController
@RequestMapping("/manage/petGoods")
@Slf4j
public class PetGoodsController {

    @Resource
    private PetGoodsService petGoodsService;

    @Value("${pet.file}")
    private String basePath;

    @Value("${pet.downloadUrl}")
    private String downloadUrl;

    @Resource
    private PetGoodsTypeService petGoodsTypeService;

    @GetMapping("list")
    public Object list(GoodsDTO goodsDTO){
        return Result.ok("success",new PageInfo<>(petGoodsService.listByPage(goodsDTO)));
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Object detail(Integer id){
        PetGoods petGoods = petGoodsService.getById(id);
        petGoods.setPicture(downloadUrl+"/"+petGoods.getId());
        return Result.ok("success",petGoods);
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @GetMapping("adminDetail/{id}")
    public Object adminDetail(@PathVariable("id") Integer id){
        PetGoods petGoods = petGoodsService.getById(id);
        petGoods.setPicture(downloadUrl+"/"+petGoods.getId());
        return Result.ok("success",petGoods);
    }

    /**
     * 添加商品
     * @param petGoods
     * @return
     */
    @PostMapping("add")
    @Transactional
    public Object add(PetGoods petGoods){
        if (petGoods.getFile()!=null && !FileUtil.uploadFile(petGoods.getFile(), basePath,petGoods)){
            return Result.error("文件上传失败");
        }
        if (!petGoodsService.save(petGoods)){
            return Result.error("添加失败");
        }
        return Result.ok("添加成功");
    }

    /**
     * 商品上下架
     * @param
     * @return
     */
    @PutMapping("upOrdown")
    public Object upOrdown(@RequestBody UpOrDownGoodsDTO upOrDownGoodsDTO){
        PetGoods petGoods = new PetGoods();
        petGoods.setId(upOrDownGoodsDTO.getId());
        //下架
        petGoods.setStatus(upOrDownGoodsDTO.getStatus());
        if (!petGoodsService.updateById(petGoods)){
            return Result.ok(upOrDownGoodsDTO.getStatus()==1?"下架成功":"上架成功");
        }
        return Result.error(upOrDownGoodsDTO.getStatus()==1?"下架失败":"上架失败");
    }

    /**
     * 商品编辑
     * @param petGoods
     * @return
     */
    @PutMapping("edit")
    public Object edit(@RequestBody PetGoods petGoods){
        if (petGoods.getFile()!=null && !FileUtil.uploadFile(petGoods.getFile(), basePath,petGoods)){
            return Result.error("文件上传失败");
        }
        if (!petGoodsService.updateById(petGoods)){
            return Result.ok("编辑成功");
        }
        return Result.error("编辑失败");
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") Integer id){
        if (!petGoodsService.removeById(id)){
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }


    /**
     * 文件预览功能
     * @param id 文件路径
     * @param response 输出
     */
    @GetMapping("fileView/{id}")
    public void fileView(@PathVariable("id") Integer id, HttpServletResponse response){
        PetGoods petGoods = petGoodsService.getById(id);
        File file = new File(basePath,petGoods.getPicture());
        if (file.exists()){
            byte[] data = null;
            try {
                FileInputStream input = new FileInputStream(file);
                data = new byte[input.available()];
                input.read(data);
                response.getOutputStream().write(data);
                input.close();
            } catch (Exception e) {
                if (log.isDebugEnabled()){
                    log.debug("文件显示发生异常..{}",e.getMessage());
                }
            }
        }else{
            return;
        }
    }



    @GetMapping("getTypeAndGoods")
    public Object getTypeAndGoods(){
        return Result.ok("success",petGoodsService.getTypeAndGoods());
    }

    @GetMapping("getGoodsTypeList")
    public Object getGoodsTypeList(){
        return Result.ok("success",petGoodsTypeService.list());
    }
}
